import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'

const routes = [
  {
    path: '/',
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'courses',
        name: 'Courses',
        component: () => import('@/views/course/AllCourses.vue'),
        meta: { title: '全部课程' }
      },
      {
        path: 'course/:id',
        name: 'CourseDetail',
        component: () => import('@/views/course/CourseDetail.vue'),
        meta: { title: '课程详情', icon: 'el-icon-s-help' }
      },
      {
        path: 'course/:id/learn',
        name: 'CourseLearn',
        component: () => import('@/views/course/CourseLearn.vue'),
        meta: { title: '课程学习', requiresAuth: true }
      },
      {
        path: 'chat',
        name: 'Chat',
        component: () => import('@/views/features/AIAssistant.vue'),
        meta: { title: 'AI助手' }
      },
      {
        path: 'learning-plan',
        name: 'LearningPlan',
        component: () => import('@/views/features/LearningPlan.vue'),
        meta: { title: '学习计划' }
      },
      {
        path: 'mind-map',
        name: 'MindMap',
        component: () => import('@/views/features/MindMap.vue'),
        meta: { title: '思维导图' }
      },
      {
        path: 'mistake-book',
        name: 'MistakeBook',
        component: () => import('@/views/features/MistakeBook.vue'),
        meta: { title: '错题本' }
      },
      {
        path: 'resources',
        name: 'Resources',
        component: () => import('@/views/features/Resources.vue'),
        meta: { title: '学习资源' }
      },
      {
        path: 'notes',
        name: 'Notes',
        component: () => import('@/views/features/Notes.vue'),
        meta: { title: '笔记本' }
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: () => import('@/views/features/Statistics.vue'),
        meta: { title: '学习统计' }
      },
      {
        path: 'reminders',
        name: 'Reminders',
        component: () => import('@/views/features/Reminders.vue'),
        meta: { title: '学习提醒' }
      },
      {
        path: 'community',
        name: 'Community',
        component: () => import('@/views/community/Community.vue'),
        meta: { title: '学习社区' }
      },
      {
        path: 'community/discussion/:id',
        name: 'DiscussionDetail',
        component: () => import('@/views/community/DiscussionDetail.vue'),
        meta: { title: '讨论详情' }
      },
      {
        path: 'community/discussion/create',
        name: 'CreateDiscussion',
        component: () => import('@/views/community/CreateDiscussion.vue'),
        meta: { title: '发起讨论', requiresAuth: true }
      },
      {
        path: 'community/question/:id',
        name: 'QuestionDetail',
        component: () => import('@/views/community/QuestionDetail.vue'),
        meta: { title: '问题详情' }
      },
      {
        path: 'community/question/create',
        name: 'CreateQuestion',
        component: () => import('@/views/community/CreateQuestion.vue'),
        meta: { title: '提问题', requiresAuth: true }
      },
      {
        path: 'community/note/:id',
        name: 'NoteDetail',
        component: () => import('@/views/community/NoteDetail.vue'),
        meta: { title: '笔记详情' }
      },
      {
        path: 'community/note/create',
        name: 'CreateNote',
        component: () => import('@/views/community/CreateNote.vue'),
        meta: { title: '写笔记', requiresAuth: true }
      },
      {
        path: 'search',
        name: 'Search',
        component: () => import('@/views/Search.vue'),
        meta: { title: '搜索结果' }
      },
      {
        path: 'user/profile',
        name: 'UserCenter',
        component: () => import('@/views/UserCenter.vue'),
        meta: { title: '个人中心', requiresAuth: true }
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Login.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: '页面不存在' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 检查是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否已登录
    const token = store.getters.token;
    if (!token) {
      // 未登录，重定向到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router
